package com.frxuexi.shop.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bumptech.glide.Glide
import com.frxuexi.lib_base.entity.ShopBeanItem
import com.frxuexi.lib_base.utils.BitmapUtils
import com.frxuexi.lib_base.utils.GlideUtils
import com.frxuexi.shop.R
import com.frxuexi.shop.databinding.ItemShopBinding

class ShopPageAdapter:PagingDataAdapter<ShopBeanItem,ShopPageAdapter.ShopViewHolder>(diff) {
    private var mOnItemClickListener: OnItemClickListener? = null
    private var mOnItemLongClickListener: OnItemLongClickListener? = null
    companion object{
        val diff=object :ItemCallback<ShopBeanItem>(){
            override fun areItemsTheSame(oldItem: ShopBeanItem, newItem: ShopBeanItem): Boolean {
               return oldItem.id==newItem.id
            }

            override fun areContentsTheSame(oldItem: ShopBeanItem, newItem: ShopBeanItem): Boolean {
                return oldItem==newItem
            }
        }
    }

    class ShopViewHolder(itemView:View):ViewHolder(itemView){
        val binding=DataBindingUtil.bind<ItemShopBinding>(itemView)
    }

    override fun onBindViewHolder(holder: ShopViewHolder, position: Int) {
        val item = getItem(position)!!
        holder.binding?.bean=item
//        Glide.with(holder.itemView.context).load(item.image).into(holder.binding?.shopIv!!)
//        BitmapUtils.getInstance(holder.itemView.context).loadBitmap(item.image,200,200,{
//            holder.binding?.shopIv?.setImageBitmap(it)
//        })
        GlideUtils.loadRoundImage(item.image,holder.binding?.shopIv!!)

        holder.itemView.setOnClickListener {
            this.mOnItemClickListener?.onItemClick(position,item)
        }

        holder.itemView.setOnLongClickListener {
            mOnItemLongClickListener?.onItemLongClick(item)
            return@setOnLongClickListener true
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ShopViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_shop, null)
        return ShopViewHolder(view)
    }

    fun setOnItemClickListener(listener: OnItemClickListener){
        this.mOnItemClickListener=listener
    }

    fun setOnItemLongClickListener(listener: OnItemLongClickListener){
        this.mOnItemLongClickListener=listener
    }
}

interface OnItemClickListener{
    fun onItemClick(position: Int,item:ShopBeanItem)
}

interface OnItemLongClickListener{
    fun onItemLongClick(item: ShopBeanItem):Boolean
}